package com.mingdao.lhy.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mingdao.lhy.mapper.signup.SignUpStudentMapper2;
import com.mingdao.lhy.mapper.wish.MiddleSchoolMapper;
import com.mingdao.lhy.mapper.wish.SignUpStudentMapper;
import com.mingdao.lhy.mapper.wish.XueJiMapper;
import com.mingdao.lhy.pojo.MiddleSchool;
import com.mingdao.lhy.pojo.SignUpStudent;
import com.mingdao.lhy.pojo.XueJi;
import com.mingdao.lhy.pojo.signup.SignUpStudent2;

@Service
public class JuniorServiceImpl implements JuniorService {

	private static Logger log = LoggerFactory.getLogger(JuniorServiceImpl.class);

	@Autowired
	private SignUpStudentMapper signUpStudentMapper;

	@Autowired
	private SignUpStudentMapper2 signUpStudentMapper2;

	@Autowired
	private MiddleSchoolMapper middleSchoolMapper;

	@Autowired
	private XueJiMapper xueJiMapper;

	/**
	 * 根据学校代码返回学校信息
	 */
	@Override
	public MiddleSchool getJuniorById(String byzxdm) {
		MiddleSchool zkw = middleSchoolMapper.getOne(byzxdm);
		return zkw;
	}

	@Override
	public int changePassword(MiddleSchool middleSchool) {
		int result = middleSchoolMapper.updatePasswordByPrimaryKey(middleSchool);
		return result;
	}

	@Override
	public List<SignUpStudent> getWishedStudentByByzxdmWishedHigh(String byzxdm, String bmh) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("bmh", bmh);
		map.put("byzxdm", byzxdm);
		List<SignUpStudent> students = signUpStudentMapper.getWishedStudentByByzxdmWishedHigh(map);
		return students;
	}

	@Override
	public List<SignUpStudent> getWishedStudentByByzxdmNotWishedHigh(String byzxdm, String bmh) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("bmh", bmh);
		map.put("byzxdm", byzxdm);
		List<SignUpStudent> students = signUpStudentMapper.getWishedStudentByByzxdmNotWishedHigh(map);
		return students;
	}

	@Override
	public List<SignUpStudent> getWishedStudentByByzxdmWishedMiddle(String byzxdm, String bmh) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("bmh", bmh);
		map.put("byzxdm", byzxdm);
		List<SignUpStudent> students = signUpStudentMapper.getWishedStudentByByzxdmWishedMiddle(map);
		return students;
	}

	@Override
	public List<SignUpStudent> getWishedStudentByByzxdmNotWishedMiddle(String byzxdm, String bmh) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("bmh", bmh);
		map.put("byzxdm", byzxdm);
		List<SignUpStudent> students = signUpStudentMapper.getWishedStudentByByzxdmNotWishedMiddle(map);
		return students;
	}

	/**
	 * 根据毕业中学代码查询所有已报名学生信息
	 */
	@Override
	public List<SignUpStudent2> findAllSignUpStudentsByByzxdm(String byzxdm) {

		List<SignUpStudent2> signUpStudents = signUpStudentMapper2.listByByzxdm(byzxdm);
		return signUpStudents;
	}

	/**
	 * 根据待处理状态查询所有报名学生信息
	 */
	@Override
	public List<SignUpStudent2> findAllSignUpStudentsByDclzt(String dclzt) {
		List<SignUpStudent2> signUpStudents = signUpStudentMapper2.listByDclzt(dclzt);
		return signUpStudents;
	}

	/**
	 * 根据毕业中学代理查询所有未报名的学生学籍信息
	 */
	@Override
	public List<SignUpStudent2> findNotSignUpStudentsByByzxdm(String byzxdm) {
		List<SignUpStudent2> signUpStudents = new ArrayList<SignUpStudent2>();
		List<XueJi> xueJis = xueJiMapper.listByNotSignUpByByzxdm(byzxdm);
		for (XueJi xueJi : xueJis) {
			// 学籍号 报名号 姓名 身份证件号 班级名称 性别 民族 出生年月 学生状态 问题类型 错误类型

			SignUpStudent2 signUpStudent = new SignUpStudent2();
			signUpStudent.setXjh(xueJi.getXjh());
			signUpStudent.setSfzh(xueJi.getSfzh());
			signUpStudent.setXm(xueJi.getXm());
			signUpStudent.setBjdm(xueJi.getBjdm());
			signUpStudent.setXb(xueJi.getXb());
			signUpStudent.setMz(xueJi.getMz());
			signUpStudent.setCsrq(xueJi.getCsrq());
			signUpStudent.setByzxdm(xueJi.getByzxdm());

			signUpStudents.add(signUpStudent);
		}
		return signUpStudents;
	}

	/**
	 * 根据身份证号更新报名学生为待处理状态
	 */
	@Override
	public int setDclztBySfzh(List<SignUpStudent2> signUpStudent2s) {
		int result = 0;
		for (SignUpStudent2 s2 : signUpStudent2s) {
			SignUpStudent2 signUpStudent = new SignUpStudent2();
			signUpStudent.setSfzh(s2.getSfzh());
			signUpStudent.setDclzt("2");
			signUpStudent.setByzxdm(s2.getByzxdm());
			signUpStudent.setByzxmc(s2.getByzxmc());
			int ret = signUpStudentMapper2.updateDclztByPrimaryKey(signUpStudent);
			if (1 == ret) {
				result++;
			}
		}
		return result;
	}

	/**
	 * 根据身份证号更新报名学生为确定状态
	 */
	@Override
	public int setConfirmBySfzh(List<SignUpStudent2> signUpStudent2s) {
		int result = 0;
		for (SignUpStudent2 s2 : signUpStudent2s) {
			SignUpStudent2 signUpStudent = new SignUpStudent2();
			signUpStudent.setSfzh(s2.getSfzh());
			signUpStudent.setDclzt("1");
			signUpStudent.setByzxdm(s2.getByzxdm());
			signUpStudent.setByzxmc(s2.getByzxmc());
			signUpStudent.setJdd(s2.getJdd());
			int ret = signUpStudentMapper2.updateDclztByPrimaryKey(signUpStudent);
			if (1 == ret) {
				result++;
			}
		}
		return result;

	}

}
